using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Cmc.Modbus.Base
{
    public class AddressReadValue
    {

        public AddressReadValue(int address, byte low, byte high)
        {
            Address = address;
            Low = low;
            High = high;
        }

        public int Address { get; set; }

        public byte High { get; set; }

        public byte Low { get; set; }

        public bool ValueBool
        {
            get
            {
                return ValueInt > 0;
            }
        }

        public int ValueInt
        {
            get
            {
                return High * 256 + Low;
            }
        }

        [JsonIgnore]
        public BitArray ValueBit
        {
            get
            {
                return OtherHelper.ToBitArray(Low, High);
            }
        }

        public bool[] ValueBitBools
        {
            get
            {
                var bits = ValueBit;
                bool[] result = new bool[bits.Length];
                for (int i = 0; i < bits.Length; i++)
                {
                    result[i] = bits[i];
                }

                return result;
            }
        }
        public int[] ValueBitInts
        {
            get
            {
                var bits = ValueBit;
                int[] result = new int[bits.Length];
                for (int i = 0; i < bits.Length; i++)
                {
                    result[i] = bits[i] ? 1 : 0;
                }

                return result;
            }
        }


    }
}
